home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / tutor / cptuts22.arj / INHERIT4.CPP < prev    next >
C/C++ Source or Header  |  1992-01-20  |  3KB  |  126 lines

  1.                                   // Chapter 8 - Program 4
  2. #include <iostream.h>
  3.  
  4. class vehicle {
  5. protected:
  6.    int wheels;
  7.    float weight;
  8. public:
  9.    void initialize(int in_wheels, float in_weight);
  10.    int get_wheels(void) {return wheels;}
  11.    float get_weight(void) {return weight;}
  12.    float wheel_loading(void) {return weight/wheels;}
  13. };
  14.  
  15.  
  16.  
  17.  
  18. class car : public vehicle {
  19. private:
  20.    int passenger_load;
  21. public:
  22.    void initialize(int in_wheels, float in_weight, int people = 4);
  23.    int passengers(void) {return passenger_load;}
  24. };
  25.  
  26.  
  27.  
  28.  
  29. class truck : public vehicle {
  30. private:
  31.    int passenger_load;
  32.    float payload;
  33. public:
  34.    void init_truck(int how_many = 2, float max_load = 24000.0);
  35.    float efficiency(void);
  36.    int passengers(void) {return passenger_load;}
  37. };
  38.  
  39.  
  40.  
  41.  
  42. main()
  43. {
  44. vehicle unicycle;
  45.  
  46.    unicycle.initialize(1, 12.5);
  47.    cout << "The unicycle has " <<
  48.                                 unicycle.get_wheels() << " wheel.\n";
  49.    cout << "The unicycle's wheel loading is " <<
  50.          unicycle.wheel_loading() << " pounds on the single tire.\n";
  51.    cout << "The unicycle weighs " <<
  52.                              unicycle.get_weight() << " pounds.\n\n";
  53.  
  54. car sedan;
  55.  
  56.    sedan.initialize(4, 3500.0, 5);
  57.    cout << "The sedan carries " << sedan.passengers() <<
  58.                                                     " passengers.\n";
  59.    cout << "The sedan weighs " << sedan.get_weight() << " pounds.\n";
  60.    cout << "The sedan's wheel loading is " <<
  61.                     sedan.wheel_loading() << " pounds per tire.\n\n";
  62.  
  63. truck semi;
  64.  
  65.    semi.initialize(18, 12500.0);
  66.    semi.init_truck(1, 33675.0);
  67.    cout << "The semi weighs " << semi.get_weight() << " pounds.\n";
  68.    cout << "The semi's efficiency is " <<
  69.                           100.0 * semi.efficiency() << " percent.\n";
  70. }
  71.  
  72.  
  73.  
  74.  
  75.               // initialize to any data desired
  76. void
  77. vehicle::initialize(int in_wheels, float in_weight)
  78. {
  79.    wheels = in_wheels;
  80.    weight = in_weight;
  81. }
  82.  
  83.  
  84.  
  85. void
  86. car::initialize(int in_wheels, float in_weight, int people)
  87. {
  88.    passenger_load = people;
  89.    wheels = in_wheels;
  90.    weight = in_weight;
  91. }
  92.  
  93.  
  94.  
  95.  
  96. void
  97. truck::init_truck(int how_many, float max_load)
  98. {
  99.    passenger_load = how_many;
  100.    payload = max_load;
  101. }
  102.  
  103.  
  104.  
  105. float
  106. truck::efficiency(void)
  107. {
  108.    return payload / (payload + weight);
  109. }
  110.  
  111.  
  112.  
  113.  
  114. // Result of execution
  115. //
  116. // The unicycle has 1 wheel.
  117. // The unicycle's wheel loading is 12.5 pounds on the single tire.
  118. // The unicycle weighs 12.5 pounds.
  119. //
  120. // The sedan carries 5 passengers.
  121. // The sedan weighs 3500 pounds.
  122. // The sedan's wheel loading is 875 pounds per tire.
  123. //
  124. // The semi weighs 12500 pounds.
  125. // The semi's efficiency is 72.929072 percent.
  126.